<template>
	<wsPage title="平台资金流水">
		<div slot="page_operate">
			<Button  @click="refresh"><Icon type="refresh"></Icon><span>刷新</span></Button>
		</div>
		<wsSmPanel :page="table.page" @simple-query="query" @page-change="query">
			<div slot="query_form">
				<Form :model="queryForm" :label-width="100" inline>
					<FormItem label="收支类型">
							<Select clearable v-model="queryForm.type" placeholder="请选择" class="ws-form-select">
									<Option value="1">收入</Option>
									<Option value="2">支出</Option>
							</Select>
					</FormItem>
					<FormItem label="流水类型">
							<Select clearable v-model="queryForm.flowType" placeholder="请选择" class="ws-form-select">
									<Option value="1">收款</Option>
									<Option value="2">提现</Option>
									<Option value="3">退款</Option>
							</Select>
					</FormItem>
					<FormItem label="业务类型">
							<Select clearable v-model="queryForm.bizType" placeholder="请选择" class="ws-form-select">
									<Option value="1">商城订单</Option>
									<Option value="2">健康订单</Option>
									<Option value="3">商城退款退货订单</Option>
									<Option value="4">健康退款订单</Option>
									<Option value="5">提现</Option>
							</Select>
					</FormItem>
                    <FormItem label="所属店铺">
			            <Select clearable v-model="queryForm.tenantId" placeholder="请选择" class="ws-form-select">
		                	<Option v-for="item in merchantData" :value="item.tenantId" :key="item.id">{{ item.shop }}</Option>
		            	</Select>
			        </FormItem><br>
					<FormItem label="时间区间">
						<Row>
			                <Col span="11">
			                    <FormItem>
			                        <DatePicker type="date" placeholder="查询开始时间" v-model="queryForm.searchStartTime" @on-change="dateConfirm"></DatePicker>
			                    </FormItem>
			                </Col>
			                <Col span="2" style="text-align: center">-</Col>
			                <Col span="11">
			                    <FormItem>
			                        <DatePicker type="date" placeholder="查询结束时间" v-model="queryForm.searchEndTime" @on-change="dateConfirm"></DatePicker>
			                    </FormItem>
			                </Col>
			            </Row>      
			        </FormItem>
			        <!-- <Button  @click="exportExcel"><span>导出</span></Button> -->
				</Form>
			</div>
			<div slot="list_data">
				<Table border :columns="table.columns" :data="table.data"></Table>
			</div>
		</wsSmPanel>

	</wsPage>
</template>

<script>
import Util from '../../../common/util.js';
	export default {
		data() {
			return {
				queryForm: {
					tenantId: '',
					type: '',
					flowType: '',
					bizType: '',
					searchStartTime: '',
					searchEndTime: ''
				},
				//所属店铺
				merchantData:[],
				table: {
					columns: [{
                        type: 'selection',
                        width: 60,
                        align: 'center',
                        fixed: 'left'
                    },
					{
						title: '流水号',
						key: 'sn',
	          align: 'center'
					},
					{
						title: '商户名称',
						key: 'shop',
	          align: 'center',
						render: (h, params) => {
							return h('span',{

							},params.row.shop == null?"非商户":params.row.shop)
						}
					},
					{
						title: '业务类型',
						key: 'bizType',
	          align: 'center',
						render: (h, params) => {
							return h('span',{

							},this.checkBizType(params.row.bizType))
						}
					},
					{
						title: '收支类型',
						key: 'type',
	          align: 'center',
						render: (h, params) => {
							return h('span',{

							},params.row.type == "1"?"收入":"支出")
						}
					},
					{
						title: '流水类型',
						key: 'flowType',
	          align: 'center',
						render: (h, params) => {
							return h('span',{

							},this.checkFlowType(params.row.flowType))
						}
					},
					{
						title: '流水金额',
						key: 'flowAmount',
	          align: 'center'
					},
					{
						title: '余额',
						key: 'balance',
	          align: 'center'
					},
					{
						title: '创建日期',
						key: 'createDate',
	          align: 'center',
                        fixed: 'right'
					}],

					data: [],
					page: {
						total: 0, //总条数
						pageNum: 1, //当前页
						pageSize: 10 //每页显示条数
					}
				}
			}
		},

		computed: {

		},
		mounted() {
			this.init();
		},
		methods: {
			/**
			 * 判断流水类型
			 */
			checkFlowType(flowType){
				if(flowType == 1) return '收款';
				if(flowType == 2) return '提现';
				if(flowType == 3) return '退款';
			},
			/**
			 * 判断业务类型
			 */
			checkBizType(bizType){
				if(bizType == 1) return '商城订单';
				if(bizType == 2) return '健康订单';
				if(bizType == 3) return '商城退款退货订单';
				if(bizType == 4) return '健康退款订单';
				if(bizType == 5) return '提现';
			},

			//查询开始时间段必填
			dateConfirm(){
				let searchStartTime = this.queryForm.searchStartTime;
				let searchEndTime = this.queryForm.searchEndTime;
				if (searchStartTime != null ||searchStartTime != '') {
					searchEndTime = new Date();
				} else if (searchEndTime != null || searchEndTime !='') {
					searchStartTime = searchEndTime;
				}
			},

			init() {
				this.query();
				this.queryMerchant();
			},
			//刷新
			refresh(){
				this.queryForm.type = '';
				this.queryForm.flowType = '';
				this.queryForm.bizType = '';
				this.queryForm.searchStartTime = '';
				this.queryForm.searchEndTime = '';
				this.queryForm.tenantId = '';
				this.table.page.pageNum = 1;
				this.init();
			},
			/**
			 * 表单查询
			 */
			query() {
				let pageSize = this.table.page.pageSize;
				let pageNum = this.table.page.pageNum;
				const _this = this;
				this.axios.post('/ws/ops/platformAccountRecords?pageNum=' + pageNum + '&pageSize=' + pageSize, this.queryForm).then(response => {
					var res = response.data;
					var resMeta = res.meta;
					if (0 == resMeta.code) {
						_this.buildPageData(res.data);
					} else {
						_this.$Message.warning(resMeta.message);
					}
				});
			},
			/**
			 * 构建表格数据
			 */
			buildPageData(data_) {
				this.table.data = data_.list;
				this.table.page.total = data_.total;
			},
			//根据时间导出Excel
			exportExcel(){
				if(this.queryForm.searchStartTime == "" ||this.queryForm.searchEndTime == ""){
					this.$Message.warning("请选择时间区间！");
					return;
				}
				var startTime = Util.date.formatDate(this.queryForm.searchStartTime, 'yyyy-MM-dd');
				var endTime = Util.date.formatDate(this.queryForm.searchEndTime, 'yyyy-MM-dd');
				var timeGap = new Date(endTime).getTime() - new Date(startTime).getTime();
				if(Math.floor(timeGap/(24*3600*1000)) >= 31){
					this.$Message.warning("时间差不可大于一个月！");
					return;
				}
				window.location.href = "/ws/api/ops/platformAccountRecords/exportExcel/" + startTime +"/"+ endTime;
				this.refresh();
			},

			/**
			 * 查询所属店铺
			 */
			queryMerchant(){
	    		const _this = this;
				this.axios.get("/ws/ms/merchants/condition").then(response => {
					var res = response.data;
					var resMeta = res.meta;
					if (0 == resMeta.code) {
						_this.merchantData = res.data;
					}
				});    		
	    	}
		}
	}
</script>

<style scoped>
	.ws-form-select {
		width: 162px;
	}
</style>
